<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>组件分离</title>
    <script src="../js/vue.js"></script>
    <script src="../js/axios.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        #app {
            width: 600px;
            margin: 50px auto;
        }

        #app .item {
            width: 100px;
            height: 50px;
            background-color: aquamarine;
            color: black;
            text-align: center;
            line-height: 50px;
            float: left;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="headers">
            <div class="item"><input type="checkbox" :checked="checkedAll" @change="setCheckedAll">全选</div>
            <div class="item">商品名称</div>
            <div class="item">商品数量</div>
            <div class="item">商品价格</div>
            <div class="item">小计</div>
            <div class="item">删除</div>
        </div>

        <div class="details" v-for="item in list">
            <div class="item"><input type="checkbox" v-model="item.checked" />{{item.id}}</div>
            <div class="item">{{item.goodsname}}</div>
            <div class="item">
                <button v-show="item.goodsnum>1" @click="calc(item,-1)">-</button>
                {{item.goodsnum}}
                <button @click="calc(item,1)">+</button>
            </div>
            <div class="item">{{item.goodsprice}}</div>
            <div class="item">{{item.goodsnum*item.goodsprice}}</div>
            <div class="item" @click="del(item.id)">删除</div>
        </div>

        <div class="footers">
            <div class="item" @click="delAll">批量删除</div>
            <div class="item"></div>
            <div class="item">{{totalnum}}</div>
            <div class="item"></div>
            <div class="item">{{totalprice}}</div>
            <div class="item">结算</div>
        </div>


    </div>




    <!-- <template id="headers">


    </template>

    <template id="details">


    </template>

    <template id="footers">


    </template> -->

</body>
<script>
    // let headers = {
    //     template: "#headers",
    // };

    // let details = {
    //     template: "#details",
    // };

    // let footers = {
    //     template: "#footers",
    // };

    new Vue({
        el: "#app",
        data: {
            checkedAll: false,
            list: [
                { id: 1, goodsname: "商品一", goodsnum: 1, goodsprice: 100, checked: false },
                { id: 2, goodsname: "商品二", goodsnum: 2, goodsprice: 100, checked: false },
                { id: 3, goodsname: "商品三", goodsnum: 3, goodsprice: 100, checked: false },
                { id: 4, goodsname: "商品四", goodsnum: 4, goodsprice: 100, checked: false }
            ]
        },
        watch: {
            list: {
                deep: true,
                handler(newList) {
                    // console.log(newList);
                    if (newList.length) {
                        this.checkedAll = newList.every(item => item.checked);
                    } else {
                        this.checkedAll = false;
                    }
                }
            }
        },
        methods: {
            calc(item, num) {
                item.goodsnum = item.goodsnum + num;
                // console.log(item.goodsnum);
            },
            del(id) {
                this.list = this.list.filter(item => item.id != id);
            },
            delAll() {
                this.list = this.list.filter(item => !item.checked);
            },
            setCheckedAll(e) {
                let flag = e.target.checked;
                this.list.forEach(item => {
                    item.checked = flag;
                })
            }
        },
        computed: {
            totalnum() {
                // return item.goodsnum
                let selectList = this.list.filter(item => item.checked);
                // console.log(selectList);//被选择的数据
                if (selectList.length) {
                    // console.log(selectList.map(item => item.goodsnum).reduce((a, b) => a + b))
                    return selectList.map(item => item.goodsnum).reduce((a, b) => a + b)
                }
                return 0;
            },
            totalprice() {
                // return item.goodsnum * item.goodsprice;
                let selectList = this.list.filter(item => item.checked);
                if (selectList.length) {
                    return selectList.map(item => item.goodsnum * item.goodsprice).reduce((a, b) => a + b);
                }
                return 0;
            },
        }


        // components: {
        //     headers,
        //     details,
        //     footers
        // }
    })

</script>

</html>